<template>
	<view class="footer-menu">
		<view class="menu-item" @click="goHome">
			<image class="menu-icon" mode="scaleToFill"
				:src="currentRoute === 'pages/index/index' ? '/static/img/homes.jpg' : '/static/img/home.jpg'"></image>
			<text class="menu-item-text">首页</text>
		</view>
		<view class="menu-item" @click="goContact">
			<image class="menu-icon" mode="scaleToFill"
				:src="currentRoute === 'pages/chat/list' ? '/static/img/contacts.jpg' : '/static/img/contact.jpg'">
			</image>
			<text class="menu-item-text">通讯录</text>
		</view>
		<view class="menu-item" @click="goUser">
			<image class="menu-icon" mode="scaleToFill"
				:src="currentRoute === 'pages/user/index' ? '/static/img/users.jpg' : '/static/img/user.jpg'"></image>
			<text class="menu-item-text">我</text>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'Footer',
		data() {
			return {
				httpUrl: "https://www.fengjian123.com/service9501/",
				// httpUrl: "http://192.168.200.128:9501/",
				currentRoute: '',
			}
		},
		mounted() {
			this.$nextTick(() => {
				this.wxlogin();
				this.getPage();
			})
		},
		methods: {
			getPage: function() {
				setTimeout(() => {
					let _this = this;
					let res = getCurrentPages();
					_this.currentRoute = res[res.length - 1].route;
				}, 200)
			},
			goHome: function() {
				let _this = this;
				if (_this.currentRoute !== 'pages/index/index') {
					uni.reLaunch({
						url: '/pages/index/index',
					});
				}
			},
			goContact: function() {
				let _this = this;
				if (_this.currentRoute !== 'pages/chat/list') {
					uni.reLaunch({
						url: '/pages/chat/list',
					});
				}
			},
			goUser: function() {
				let _this = this;
				if (_this.currentRoute !== 'pages/user/index') {
					uni.reLaunch({
						url: '/pages/user/index',
					});
				}
			},
			wxlogin: function() {
				let _this = this;
				uni.getStorage({
					key: 'cookie',
					success: function(res) {
						console.log("获取缓存中...");
						if (res.data.id) {
							console.log("获取缓存成功");
						} else {
							console.log("获取缓存失败，用户id丢失");
							this.logging();
						}
					},
					fail: (res) => {
						console.log("获取缓存失败，需要重新登录");
						this.logging();
					},
				});
			},
			logging: function() {
				let _this = this;
				uni.login({
					"provider": "weixin",
					"onlyAuthorize": true,
					success: function(event) {
						uni.request({
							url: _this.httpUrl + 'login/wxlogin',
							method: 'POST',
							data: {
								code: event.code
							},
							success: (res) => {
								console.log(res.data.msg);
								if (res.data.err == 0) {
									uni.setStorageSync('cookie', res
										.data.data);
									console.log("缓存成功");
								} else {
									setInterval(_this.wxlogin, 3000);
								}
							},
							fail: function(err) {
								console.log("应该是网络问题..");
								console.log(err);
								setInterval(_this.wxlogin, 3000);
							}
						});
					},
					fail: function(err) {
						console.log(err);
					}
				})
			}
		}
	};
</script>